import request from '@/utils/request'
import { getQueryParams, ossType } from '@/utils/dic/uploadType.js'

// 上传文件
export function uploadfile(data) {
  return request({
    url: '/erp/dataoperation/uploadfile',
    method: 'post',
    data: data
  }).then(res => {
    var imageUrl = res && res.data
    var success=res&&res.success;
    return { data: imageUrl,success }
    // endregion
  })
}

export function uploadfile_dingdingyunpan(formData,code) {
  return request({
  // /approval/uploadfile
    url: `/ding/approval/uploadfile?code=${code||''}`,
    method: 'post',
    data: formData
  })
}


function handerEncompForName(imageUrl) {
  var index = imageUrl && imageUrl.lastIndexOf && imageUrl.lastIndexOf('/')
  if (index >= 0) {
    var strbefore = imageUrl.substr(0, index)
    var strafter = imageUrl.substr(index + 1, imageUrl.length)
    var wenhanstrindex = strafter.indexOf('?')
    if (wenhanstrindex >= 0) {
      var wenhaobefore = strafter.substr(0, wenhanstrindex)
      var wenhaoafter = strafter.substr(wenhanstrindex + 1, strafter.length)
      if (wenhaoafter) {
        strafter = `${encodeURIComponent(wenhaobefore)}?${wenhaoafter}`
      } else {
        strafter = `${encodeURIComponent(wenhaobefore)}`
      }
    }

    if (strafter) {
      return `${strbefore}/${strafter}`
    }
  }
  return imageUrl
}

//str的路径osstype得到不同文件的网页路径
export function completeFilePath(imageUrl, showOrigin = false) {
  var hasHttp = imageUrl && imageUrl.indexOf('http') == 0
  if(hasHttp/*如果http开头 直接退出逻辑*/){
  return imageUrl;
  }

  var osstype = getQueryParams(imageUrl, 'osstype')
  if (!osstype) {
    osstype = getQueryParams(imageUrl, 'ossType')
  }
  //如果还是没有 默认 osstype=szqerp-prod
  if (!osstype) {
    osstype = 'szqerp-prod'
  }
  if ((!hasHttp) && osstype) {
    imageUrl = `${ossType[osstype]}/${handerEncompForName(imageUrl)}`
  }

  //规定图片大小
  //console.log('isimage(imageUrl)',imageUrl,isimage(imageUrl));
  if (!showOrigin && isimage(imageUrl) && osstype === 'aliyun'/*如果是aliyun的图片 后面加图片大小限制*/) {
    //&x-oss-process=image/resize,h_300,w_300
    imageUrl += '&x-oss-process=image/resize,h_800,w_800'
  }
  /*如果需要显示 原生的图片*/
  if(showOrigin){
    imageUrl=imageUrl.replace('&x-oss-process=image/resize,h_800,w_800','')
  }
  return imageUrl
}

function isimage(fileName) {
  if (!/\.(jpg|jpeg|png|GIF|JPG|PNG)/.test(fileName)) {
    return false
  } else {
    return true
  }

}

// 导入数据
export function ajaxImportData(data) {
  return request({
    url: '/erp/dataoperation/add',
    method: 'post',
    data: data,
    timeout: 1000 * 60 * 5
  })
}


export function ajaxImportDataForMes(data) {
  return request({
    url: '/mes/data/import/add',
    method: 'post',
    data: data,
    timeout: 1000 * 60 * 5
  })
}


export function getImportListForMes(data) {
  return request({
    url: '/mes/data/import/getlist',
    method: 'post',
    data: data
  })
}

// 重导excel
export function retry(id) {
  return request({
    url: '/mes/data/import/retrybyid',
    method: 'get',
    params: {id:id}
  })
}


export function uploadImageAjax(data) {
  return request({
    url: '/erp/dataoperation/uploadfile',
    method: 'post',
    data,
  }).then(res=>res.data);
}



